/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.equipment.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.common.utils.I18nUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.equipment.entity.EleEquipment;
import com.thinkgem.jeesite.modules.equipment.entity.EleEquipmentCommand;
import com.thinkgem.jeesite.modules.equipment.service.EleEquipmentCommandService;
import com.thinkgem.jeesite.modules.equipment.service.EleEquipmentNocheckService;
import com.thinkgem.jeesite.modules.equipment.service.EleEquipmentService;
import com.thinkgem.jeesite.modules.messages.service.EleEquipmentMessagesService;
import com.thinkgem.jeesite.modules.publish.entity.EleSourceGroupPublish;
import com.thinkgem.jeesite.modules.publish.entity.EleTempletPublish;
import com.thinkgem.jeesite.modules.publish.service.EleSourceGroupPublishService;
import com.thinkgem.jeesite.modules.publish.service.EleTempletPublishService;
import com.thinkgem.jeesite.modules.templet.service.EleTempletService;

/**
 * 日志命令Controller
 * @author unnamed
 * @version 2017-07-05
 */
@Controller
@RequestMapping(value = "${adminPath}/equipment/eleEquipmentCommand")
public class EleEquipmentCommandController extends BaseController {
	@Autowired
	private EleEquipmentService eleEquipmentService;
  
	@Autowired
	private EleTempletService templetService;
	@Autowired
	private EleTempletPublishService templetPublishService;
	@Autowired
	private EleSourceGroupPublishService eleSourceGroupPublishService;
	@Autowired
	private EleEquipmentCommandService eleEquipmentCommandService;
	
	@ModelAttribute
	public EleEquipmentCommand get(@RequestParam(required=false) String id) {
		EleEquipmentCommand entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = eleEquipmentCommandService.get(id);
		}
		if (entity == null){
			entity = new EleEquipmentCommand();
		}
		return entity;
	}
	
	@RequiresPermissions("equipment:eleEquipmentCommand:view")
	@RequestMapping(value = {"list", ""})
	public String list(EleEquipmentCommand eleEquipmentCommand, HttpServletRequest request, HttpServletResponse response, Model model) {
		EleEquipment eleEquipment = new EleEquipment();
		eleEquipment.setCode(eleEquipmentCommand.getMac());
		List<EleEquipment> availabeList = eleEquipmentService.findList(eleEquipment);
		eleEquipment.setIsPublish("2");
		
		List<EleEquipment> monitorList = eleEquipmentService.findList(eleEquipment);
		List<EleEquipmentCommand> commandList = eleEquipmentCommandService.findListByEquipments(availabeList, EleEquipmentCommand.DEL_FLAG_NORMAL);
		List<EleSourceGroupPublish> groupList = eleSourceGroupPublishService.findListByEquipments(availabeList, EleSourceGroupPublish.DEL_FLAG_NORMAL);
		List<EleTempletPublish> templetList = templetPublishService.findListByEquipments(availabeList, EleSourceGroupPublish.DEL_FLAG_NORMAL);
		
		model.addAttribute("monitors", monitorList);
		model.addAttribute("commands", commandList);
		model.addAttribute("groups", groupList);
		model.addAttribute("templets", templetList);

		return "modules/equipment/eleEquipmentCommandList";
	}

	@RequiresPermissions("equipment:eleEquipmentCommand:view")
	@RequestMapping(value = "form")
	public String form(EleEquipmentCommand eleEquipmentCommand, Model model) {
		model.addAttribute("eleEquipmentCommand", eleEquipmentCommand);
		return "modules/equipment/eleEquipmentCommandForm";
	}

	@RequiresPermissions("equipment:eleEquipmentCommand:edit")
	@RequestMapping(value = "save")
	public String save(EleEquipmentCommand eleEquipmentCommand, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, eleEquipmentCommand)){
			return form(eleEquipmentCommand, model);
		}
		eleEquipmentCommandService.save(eleEquipmentCommand);
		String locale = I18nUtils.getLocaleStr();
		String msg = "保存日志命令成功";
		if(Global.US.equals(locale)){
			msg = "Save log command success";
		}
		addMessage(redirectAttributes, msg);
		return "redirect:"+Global.getAdminPath()+"/equipment/eleEquipmentCommand/?repage";
	}
	
	@RequiresPermissions("equipment:eleEquipmentCommand:edit")
	@RequestMapping(value = "delete")
	public String delete(EleEquipmentCommand eleEquipmentCommand, RedirectAttributes redirectAttributes) {
		eleEquipmentCommandService.delete(eleEquipmentCommand);
		String locale = I18nUtils.getLocaleStr();
		String msg = "删除日志命令成功";
		if(Global.US.equals(locale)){
			msg = "Delete log command success";
		}
		addMessage(redirectAttributes, msg);
		return "redirect:"+Global.getAdminPath()+"/equipment/eleEquipmentCommand/?repage";
	}
	
	@RequiresPermissions("equipment:eleEquipmentCommand:edit")
	@RequestMapping(value = "cancel")
	public String cancel(String commandId,String type, RedirectAttributes redirectAttributes) {
		switch(type){
		case "monitor":
			eleEquipmentService.updatePublishStatus(commandId, "4");
			break;
		case "command":
			EleEquipmentCommand command = eleEquipmentCommandService.get(commandId);
			eleEquipmentCommandService.delete(command);
			break;
		case "group":
			eleSourceGroupPublishService.updatePublishStatus(commandId, "4");
			break;
		case "template":
			templetPublishService.updatePublishStatus(commandId, "4");
			break;
		default:
			break;
		}
		String locale = I18nUtils.getLocaleStr();
		String msg = "撤销指令成功";
		if(Global.US.equals(locale)){
			msg = "Revocation of instructions";
		}
		addMessage(redirectAttributes, msg);
		return "redirect:"+Global.getAdminPath()+"/equipment/eleEquipmentCommand/?repage";
	}

}